####### These are codes to replicate the analysis in "Labor Market Institutions and Outward Foreign Direct Investment in OECD Countries" by Mi Jeong Shin and Chia-yi Lee ########

library(DAMisc) 
library(stargazer) 
library(ecm) 

pdata <- read.csv("data_labor&OFDI.csv") # read the data 

## Table 2 ##
m1 <- lm(lnofdi~ lnofdi.1+ud.1+lnGDPpc.1+trade.1+growth.1+I(left.1/10)+election.1+openness.1+crisis.1+eu+factor(country)+factor(year), pdata) # Model 1
stargazer(m1, type="text", t.auto=F)

m2 <- lm(lnofdi~ lnofdi.1+coord.1+lnGDPpc.1+trade.1+growth.1+I(left.1/10)+election.1+openness.1+crisis.1+eu+factor(country)+factor(year), pdata, na.action="na.exclude") # Model 2
stargazer(m2, type="text", t.auto=F)

m3 <- lm(lnofdi~ lnofdi.1+ud.1+coord.1+lnGDPpc.1+trade.1+growth.1+I(left.1/10)+election.1+openness.1+crisis.1+eu+factor(country)+factor(year), pdata) # Model 3
stargazer(m3, type="text", t.auto=F)

m4 <- lm(lnofdi~ lnofdi.1+ud.1*coord.1+lnGDPpc.1+trade.1+growth.1+I(left.1/10)+election.1+openness.1+crisis.1+eu+factor(country)+factor(year), pdata, na.action="na.exclude") # Model 4
stargazer(m4, type="text", t.auto=F)

## Figure 2 ##
par(mar=c(4,4,0.5,2))
DAintfun2(m4, c("ud.1", "coord.1"), rug=F, hist=T, scale.hist=0.3, xlab=c("Centralized Wage Bargaining", "Union Density"), ylab=c("Marginal Effect of Unionization", "Marginal Effect of Centralized Wage Bargaining"))

## Figure 3 ##
pdata$p <- predict(m4)
pdata2 <- subset(pdata, is.na(pdata$p)==F)

prd <- data.frame(lnofdi.1=pdata2$lnofdi.1, lnGDPpc.1=pdata2$lnGDPpc.1, trade.1=pdata2$trade.1, growth.1=pdata2$growth.1, polity.1=pdata2$polity.1, left.1=pdata2$left.1/10, election.1=pdata2$election.1, eu=pdata2$eu, openness.1=pdata2$openness.1, crisis.1=pdata2$crisis.1, country=pdata2$country, year=pdata2$year, ud.1 = quantile(pdata2$ud.1, na.rm=T)[1], coord.1 = 1, row.names = NULL) #LL

prd2 <- data.frame(lnofdi.1=pdata2$lnofdi.1, lnGDPpc.1=pdata2$lnGDPpc.1, trade.1=pdata2$trade.1, growth.1=pdata2$growth.1, polity.1=pdata2$polity.1, left.1=pdata2$left.1/10, election.1=pdata2$election.1, eu=pdata2$eu, openness.1=pdata2$openness.1, crisis.1=pdata2$crisis.1, country=pdata2$country, year=pdata2$year, ud.1 = quantile(pdata2$ud.1, na.rm=T)[1], coord.1 = 5, row.names = NULL) #LH

prd3 <- data.frame(lnofdi.1=pdata2$lnofdi.1, lnGDPpc.1=pdata2$lnGDPpc.1, trade.1=pdata2$trade.1, growth.1=pdata2$growth.1, polity.1=pdata2$polity.1, left.1=pdata2$left.1/10, election.1=pdata2$election.1, eu=pdata2$eu, openness.1=pdata2$openness.1, crisis.1=pdata2$crisis.1, country=pdata2$country, year=pdata2$year, ud.1 = quantile(pdata2$ud.1, na.rm=T)[5], coord.1 = 1, row.names = NULL) #HL

prd4 <- data.frame(lnofdi.1=pdata2$lnofdi.1, lnGDPpc.1=pdata2$lnGDPpc.1, trade.1=pdata2$trade.1, growth.1=pdata2$growth.1, polity.1=pdata2$polity.1, left.1=pdata2$left.1/10, election.1=pdata2$election.1, eu=pdata2$eu, openness.1=pdata2$openness.1, crisis.1=pdata2$crisis.1, country=pdata2$country, year=pdata2$year, ud.1 = quantile(pdata2$ud.1, na.rm=T)[5], coord.1 = 5, row.names = NULL) #HH

err <- predict(m4, newdata = prd, se.fit = TRUE)
prd$lci <- err$fit - 1.96 * err$se.fit
prd$fit <- err$fit
prd$uci <- err$fit + 1.96 * err$se.fit

err2 <- predict(m4, newdata = prd2, se.fit = TRUE)
prd2$lci <- err2$fit - 1.96 * err2$se.fit
prd2$fit <- err2$fit
prd2$uci <- err2$fit + 1.96 * err2$se.fit

err3 <- predict(m4, newdata = prd3, se.fit = TRUE)
prd3$lci <- err3$fit - 1.96 * err3$se.fit
prd3$fit <- err3$fit
prd3$uci <- err3$fit + 1.96 * err3$se.fit

err4 <- predict(m4, newdata = prd4, se.fit = TRUE)
prd4$lci <- err4$fit - 1.96 * err4$se.fit
prd4$fit <- err4$fit
prd4$uci <- err4$fit + 1.96 * err4$se.fit

mean <- c(mean(prd$fit), mean(prd2$fit), mean(prd3$fit), mean(prd4$fit))
uci <- c(mean(prd$uci), mean(prd2$uci), mean(prd3$uci), mean(prd4$uci))
lci <- c(mean(prd$lci), mean(prd2$lci), mean(prd3$lci), mean(prd4$lci))

par(mar=c(3, 4, 1, 1))
plot(c(1,2,3,4), mean, type="p", pch=19, cex=2, ylim=c(4, 12), xlim=c(0.3, 4.7), xlab="", ylab="", axes=F)
axis(1, at=c(1,2,3,4), label=c("Low unionization\n Fragmented bargaining", "Low unionization\n Centralized bargaining", "High unionization\n Fragmented bargaining", "High unionization\n Centralized bargaining"), las=1, tick=TRUE, cex.axis=0.9, tck=0.02)
segments(c(1,2,3,4), lci, c(1,2,3,4), uci, lwd=2)
axis(2)
mtext("Predicted OFDI in millions (logged)", 2, cex=1.2, line=2.5)
text(c(1,2,3,4), uci+0.3, round(mean, 2))
box()

## Table 3 ##
# ECM #
m <- lm(lnofdi~ud*coord+lnGDPpc+trade+growth+I(left/10)+election+openness+crisis+eu+factor(country)+factor(year), pdata, na.action="na.exclude") 
pdata$p2 <- predict(m)
data2 <- subset(pdata, is.na(pdata$p2)==F)
data2$interact <- data2$ud*data2$coord
data2 <- as.data.frame(data2)
xeq <- xtr <- data2[c('ud', 'coord', 'lnGDPpc', 'trade', 'growth', 'openness', 'interact')]
#xeq <- xtr <- data2[c('ud', 'coord', 'lnGDPpc', 'trade', 'growth', 'polity', 'left', 'election', 'openness', 'crisis', 'eu', 'interact')]
m5 <- ecm(data2$lnofdi, xeq, xtr) # Model 5
stargazer(m5, type="text", t.auto=F)

m6 <- lm(lnofdi~ lnofdi.1+ud.1*coord.1+lnGDPpc.1+trade.1+growth.1+I(left.1/10)+election.1+openness.1+crisis.1+eu+exrate.1+tax.1+factor(country)+factor(year), pdata, na.action="na.exclude") # Model 6
stargazer(m6, type="text", t.auto=F)

m7 <- lm(lnofdi~ lnofdi.1+ud.1*coord.1+lnGDPpc.1+trade.1+growth.1+I(left.1/10)+election.1+openness.1+crisis.1+eu+factor(country)+factor(year), subset(pdata, country!="Czech Republic" & country!="Poland" & country!="Slovakia" & country!="Slovenia" & country!="Estonia" & country!="Hungary")) # Model 7
stargazer(m7, type="text", t.auto=F)

m8 <- lm(lnofdi2~lnofdi2.1+ud.1*coord.1+lnGDPpc.1+trade.1+growth.1+I(left.1/10)+election.1+openness.1+crisis.1+eu+factor(country)+factor(year), pdata, na.action="na.exclude") # Model 8
stargazer(m8, type="text", t.auto=F)


